#include "MediaFocusControl.h"
#include <pthread.h>
#include "Log.h"
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/system_properties.h>


int simTargetChannel = -1;
int simCurrentChannel = -1;

int (*pRequestAudioFocus)(int audioType);
int (*pAbandonAudioFocus)(int audioType);
int (*pRequestSetupAudio)(int audioType,int delay,int sampleRate,int bits,int channels);
int (*pRequestTeardownAudio)(int audioType,int delay,int sampleRate,int bits,int channels);

void setSimCurrentChannel(int setterChannel)
{
	simCurrentChannel = setterChannel;
	LOGD("setSimCurrentChannel channel = %d .\n", simCurrentChannel);
}

void OnRequestAudioChannelChanged(int channel)
{

	LOGD("OnRequestAudioChannelChanged channel = %d .\n", channel);
	LOGD("OnRequestAudioChannelChanged simCurrentChannel = %d .\n", simCurrentChannel);
	if( simCurrentChannel == -1 || simCurrentChannel != channel )
	{
		simTargetChannel = channel;
		//1.当收到上层控制通道切换时，需要判读当前声音焦点状态
		bool exchanged  = ( simCurrentChannel > 0 ) && ( simCurrentChannel != channel );
		LOGD("OnRequestAudioChannelChanged exchanged = %d .\n", exchanged);
		if( AUDIO_CHANNEL_STOP == channel || exchanged)
		{
			//需要清除所有的通道
			if( pAbandonAudioFocus && simCurrentChannel > 0)
			{
				pAbandonAudioFocus(simCurrentChannel);
			}
		}
		else if( AUDIO_CHANNEL_MEDIA == channel
				|| AUDIO_CHANNEL_PHONE == channel
				|| AUDIO_CHANNEL_SPEECH == channel
				|| AUDIO_CHANNEL_NAVIGATION == channel)
		{
			//需要清除所有的通道
			if( pRequestAudioFocus )
			{
				char	speaker [ 20 ];
				memset(speaker,0,20);
				__system_property_get("sys.xqcarplay.speaker.position", speaker);
				int position = 0;
				if(strlen(speaker) > 0 )
				{
					position = atoi( speaker ); //兼容HDMI后排不能抢声音焦点
				}
				if( position == 0 || AUDIO_CHANNEL_MEDIA != channel )
				{
					pRequestAudioFocus(channel);
				}

			}
		}

	}
}

void OnAirPlayAudioFocusChanged(int audioType,int ownFocus)
{
	LOGD("OnAirPlayAudioFocusChanged audioType = %d , ownFocus = %d .\n", audioType, ownFocus);
	LOGD("OnAirPlayAudioFocusChanged simTargetChannel = %d .\n", simTargetChannel);
	LOGD("OnAirPlayAudioFocusChanged simCurrentChannel = %d .\n", simCurrentChannel);
	if( ownFocus )
	{
		if( simTargetChannel != -1 )
		{
			if(simTargetChannel != simCurrentChannel && ( simTargetChannel == audioType) )
			{
				setSimCurrentChannel(audioType);
				int delayMs = 32;
				int sampleRate = 48000;
				int bits = 16;
				int channels = 2;
				switch (audioType) {
					case AUDIO_CHANNEL_MEDIA:
						delayMs = 32;
						sampleRate = 48000;
						bits = 16;
						channels = 2;
						break;
					case AUDIO_CHANNEL_PHONE:
						delayMs = 32;
						sampleRate = 16000;
						bits = 16;
						channels = 1;
						break;
					case AUDIO_CHANNEL_SPEECH:
						delayMs = 32;
						sampleRate = 16000;
						bits = 16;
						channels = 1;
						break;
					case AUDIO_CHANNEL_NAVIGATION:
						delayMs = 32;
						sampleRate = 48000;
						bits = 16;
						channels = 1;
						break;
					default:
						break;
				}
				//请求开始播放
				if( pRequestSetupAudio )
				{
					pRequestSetupAudio(audioType,delayMs,sampleRate,bits,channels);
				}
			}
		}
	}else {
		//释放声音焦点
		if( simTargetChannel == AUDIO_CHANNEL_STOP && simTargetChannel != simCurrentChannel)
		{
			setSimCurrentChannel(simTargetChannel);
		}
		else {
			setSimCurrentChannel(AUDIO_CHANNEL_STOP);
			LOGD("OnAirPlayAudioFocusChanged resend OnRequestAudioChannelChanged  = %d .\n", simTargetChannel);
			OnRequestAudioChannelChanged( simTargetChannel );
		}

	}

}


void RegisterRequestAudioFocus(int (*pFunc)(int audioType))
{
	pRequestAudioFocus = pFunc;
}

void RegisterAbandonAudioFocus(int (*pFunc)(int audioType))
{
	pAbandonAudioFocus = pFunc;

}



void RegisterRequestSetupAudio(int (*pFunc)(int audioType,int delay,int sampleRate,int bits,int channels))
{
	pRequestSetupAudio = pFunc;
}


void RegisterRequestTeardownAudio(int (*pFunc)(int audioType,int delay,int sampleRate,int bits,int channels))
{
	pRequestTeardownAudio = pFunc;
}




